<?php
/**
* @package controller
* @file indexController.php
* @date 15.09.2009
* @version 1.0
* @copyright sample
* @author Harinath Reddy
* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';

class triggers_IndexController extends Cms_Controller_Action
{
	public function init()
	{
	  $this->view->siteTitle = 'Tock';
	  $this->currentPage = 1;
	  $this->resultsPerPage = 12;
			
	}
	public function indexAction()
	{
	  	
			
	}
	public function triggersPersonalAction()
	{
		if($_SESSION["sess_userid"]!="")
		{
			$memid = $_SESSION["sess_userid"];
			$this->view->params ="";
			$result= Doctrine::getTable('Triggers')->getTriggers($memid,1,12);
			//echo '<pre>';print_r($result);exit;
			$this->view->getTriggers=$result;
			$this->view->type=1;
		}
		else 
		{
			$this->_redirect("/home/");
		}
	}
	public function mailAction()
	{
		
		
		include('../lib/mail/SendEmail.php');
		sendMail('tavvaforu@gmail.com',"SSSSUBJSECT FROM TAVVA","MESSAGE FROM TESTSSS",'tavvaforu@gmail.com');
		sendMail('rajdaggubati@gmail.com',"SSSSUBJSECT FROM TAVVA","MESSAGE FROM TESTSSS",'tavvaforu@gmail.com');
		if(sendMail('tavvaforu@gmail.com',"SSSSUBJSECT FROM TAVVA","MESSAGE FROM TESTSSS",'tavvaforu@gmail.com'))
		{
			echo 'mail success';
		} else {
			echo 'error in mail';
		}
		echo phpinfo();exit;
	}
	public function sharedAction()
	{

	  	$memid = $_SESSION["sess_userid"];
		$this->view->params ="";
		$result= Doctrine::getTable('Triggers')->getSharedTriggers($memid,1,12);
		/*foreach($result['result'] as $trigger)
		{
			//$comments[]= Doctrine::getTable('Comments')->getComments($trigger['id'],1,12);
		}*/
		//echo '<pre>';print_r($result);exit;
		$this->view->getTriggers=$result;
		$this->view->type=2;
			
	}
	public function deletesharedTriggerAction()
	{
		//echo '<pre>';print_r($_POST);exit;
	  	$memid = $_SESSION["sess_userid"];
		$id = $_POST["val"];
		$action = $_POST["action"];
		//echo $id."  and  ".$action;exit;
		if($action == "trigger_id")
		{
		$result= Doctrine::getTable('TriggersRelations')->deleteTriggersByTriggerId($id);
		}
		else
		{		
			if($action == "relation_id")
			{
			$result= Doctrine::getTable('TriggersRelations')->deleteTriggersByRelationId($id);
			//echo $result;exit;
			}
		}
		//echo '<pre>';print_r($result);exit;
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);
			
	}
	public function triggersTrashAction()
	{
	  	
			
	}
	public function createAction()
	{
		$this->_helper->layout->disableLayout();
		$trigger_id = $this->_getParam("trigger_id");
		if($trigger_id!="")
		{
			$label="Share";
			//echo $trigger_id ;exit;
			$message = Doctrine::getTable('Triggers')->getTriggerByTriggerId($trigger_id);
			$message=$message['message'];
		} else {
			$message= "";
			$label="Create";
			}
			//echo '<pre>';print_r($message);exit;
			$memid = $_SESSION["sess_userid"];
			$group_type="Triggers";
			$groups = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
			//echo '<pre>';print_r($groups);exit;
			$this->view->groups= $groups;
			$this->view->message=$message;
			$this->view->label=$label;
			$this->view->count= count($groups);
			
	}
	public function personalShareAction()
	{
		$this->_helper->layout->disableLayout();
		//echo '<pre>';print_r($_POST);exit;
		$trigger_id = $this->_getParam("trigger_id");
		
			$memid = $_SESSION["sess_userid"];
			$message = Doctrine::getTable('Triggers')->getTriggerByTriggerId($trigger_id);
			$this->view->message=$message['message'];
			$groups = Doctrine::getTable('groups')->getGroupsByUserId($memid);
			//echo '<pre>';print_r($groups);exit;
			$this->view->groups= $groups;
			$this->view->count= count($groups);
			$jsonData['output']=$this->view->render('index/create.html');
			
			
	}
	public function saveAction()
	{
		include('../lib/mail/SendEmail.php');
	  	//echo '<pre>';print_r($_POST);exit;
		$memid = $_SESSION["sess_userid"];
		if($_POST['radio'] == "personal")
		{
			$result= Doctrine::getTable('Triggers')->addTriggers($_POST);
			if($result)
			{
			$content_type="triggers";
			$share_type=1;
			$activity = Doctrine::getTable('EmActivities')->addActivity($result,$content_type,$memid,$share_type);
			}
			$is_owner=1;$group_id=0;
			$r= Doctrine::getTable('TriggersRelations')->addTriggerRelation($result,$memid,$is_owner,$group_id);
			//echo $r."   ".$r1;exit;
			$this->_redirect("/triggers/index/triggers-personal");
		}
		
		else if($_POST['radio'] == "group")
		{
			//$result="";
			foreach($_POST['group'] as $group_id)
			{
				$result[]= Doctrine::getTable('GroupDetails')->getGroupDetailsByGroupId($group_id);
			}
			//echo '<pre>';print_r($result);exit;
			$Trigger_id= Doctrine::getTable('Triggers')->addTriggers($_POST);
			if($Trigger_id)
			{
			$content_type="triggers";
			$share_type=2;
			$activity = Doctrine::getTable('EmActivities')->addActivity($Trigger_id,$content_type,$memid,$share_type);
			}
			foreach($result as $r1)
			{
				foreach($r1 as $r2)
				{
					$is_owner=0;
					$res= Doctrine::getTable('TriggersRelations')->checkTriggerRelation($Trigger_id,$r2['user_id']);
					//echo '<pre>';print_r($res);exit;
					if($res['numCount']==0)
					{
						if($r2['user_id']!=$memid)
						{
							$r= Doctrine::getTable('TriggersRelations')->addTriggerRelation($Trigger_id,$r2['user_id'],$is_owner,$r2['group_id']);
							//echo $r."   ".$r1;exit;
							$notifications = Doctrine::getTable('Notifications')->getNotifications($r2['user_id']);
							if($notifications)
							{
							if($notifications[0]['others_shared_triggers'] !="off")
							{
								$loginLink = $this->CONFIG->serverAddress.'home';
								$Subject = "Trigger notification from EmotionsMirror";
								$Message = 'Hello "'.$r2['user_name'].'",<br/><br/>you have TriggerNotification  in your emotionmirror  <br/><br/>
									<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
										Emirror team';
								global $serverAddress;
								sendMail($r2['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
							}
							}
						}
					}
				}
			}
			$this->_redirect("/triggers/index/shared");
		}
		else if($_POST['radio']="public")
		{
				$Trigger_id= Doctrine::getTable('Triggers')->addTriggers($_POST);
				if($Trigger_id)
				{
				$content_type="triggers";
				$share_type=3;
				$activity = Doctrine::getTable('EmActivities')->addActivity($Trigger_id,$content_type,$memid,$share_type);
				}
				$result= Doctrine::getTable('Contacts')->getContacts($memid);
				//echo '<pre>';print_r($result);exit;
				foreach($result as $contact_id)
				{
					$is_owner=0; $group_id=0;
					$notifications = Doctrine::getTable('Notifications')->getNotifications($contact_id['contact_id']);
					//echo'<pre>';print_r($notifications['mytrigger_comments']);exit;
					$r= Doctrine::getTable('TriggersRelations')->addTriggerRelation($Trigger_id,$contact_id['contact_id'],$is_owner,$group_id);
					//echo $r."   ".$r1;exit;
					if($notifications)
					{
					if($notifications[0]['others_shared_triggers'] !="off")
					{
					$loginLink = $this->CONFIG->serverAddress.'home';
					$Subject = "Trigger notification from EmotionsMirror";
					$Message = 'Hello "'.$r2['user_name'].'",<br/><br/>you have TriggerNotification  in your emotionmirror  <br/><br/>
						<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
							Emirror team';
					global $serverAddress;
					sendMail($r2['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
					}
					}
					$r1[]= Doctrine::getTable('Contacts')->getFriendsContacts($contact_id['contact_id'],$memid);
					
				}
				//echo '<pre>';print_r($r1);exit;
				foreach($r1 as $r1)
				{
					foreach($r1 as $r2)
					{
						if($r2['contact_id']!=" ")
						{
							$is_owner=0; $group_id=0;
							$result= Doctrine::getTable('TriggersRelations')->checkTriggerRelation($Trigger_id,$r2['contact_id']);
							//echo '<pre>';print_r();exit;
							if($result['numCount']==0 && $r2['contact_id']!=$memid)
							{
								$r= Doctrine::getTable('TriggersRelations')->addTriggerRelation($Trigger_id,$r2['contact_id'],$is_owner,$group_id=0);
								//echo $r."   ".$r1;exit;
							}
							$notifications = Doctrine::getTable('Notifications')->getNotifications($r2['contact_id']);
							if($notifications)
							{
							if($notifications[0]['others_shared_triggers'] !="off")
							{
							$loginLink = $this->CONFIG->serverAddress.'home';
							$Subject = "Trigger notification from EmotionsMirror";
							$Message = 'Hello "'.$r2['user_name'].'",<br/><br/>you have TriggerNotification  in your emotionmirror  <br/><br/>
								<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
									Emirror team';
							global $serverAddress;
							sendMail($r2['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
							}
							}
						}
					}
				}
			$this->_redirect("/triggers/index/shared");	
		}
		
	}
	public function triggersViewAction()
	{
			$type=$this->_getParam('id');
			$currentPage=base64_decode($this->_getParam('current_page'));
			$trigger_id=base64_decode($this->_getParam('results_per_page'));
			//echo $trigger_id;exit;
			//$trigger_id=base64_decode($this->_getParam('current_page'));
			$memid = $_SESSION["sess_userid"];
			$read_status= Doctrine::getTable('TriggersRelations')->updateTriggerReadStatus($memid,$trigger_id,1);
			$this->view->params ="";
			if($type==2)
			{
			$result= Doctrine::getTable('Triggers')->getSharedTriggers($memid,$currentPage,12);
			$result['currentPage'] = $currentPage;
			//echo '<pre>';print_r($result);exit;
			$this->view->getTriggers=$result;
			
			$view_result= Doctrine::getTable('Triggers')->getSharedTriggersByTriggerId($memid,$trigger_id);
			//echo '<pre>';print_r($view_result);exit;
			$this->view->triggerview=$view_result;
			$comments= Doctrine::getTable('Comments')->getComments($trigger_id,1,12);
			//echo '<pre>';print_r($comments);exit;
			//echo '<pre>';print_r(date('Y-m-d H:i:s'));exit;
			$this->view->trigger_id=$trigger_id;
			$this->view->getComments=$comments;
			$this->view->type1=0;
			$this->view->type=$type;
			}
			if($type==1)
			{
			$result= Doctrine::getTable('Triggers')->getTriggersCurrent($memid,$currentPage);
			$result['currentPage'] = $currentPage;
			//echo '<pre>';print_r($result);exit;
			$this->view->getTriggers=$result;
			$view_result= Doctrine::getTable('Triggers')->getPersonalTriggersByTriggerId($memid,$trigger_id);
			//echo '<pre>';print_r($view_result);exit;
			$this->view->triggerview=$view_result;
			$comments= Doctrine::getTable('Comments')->getComments($trigger_id,1,12);
			//echo '<pre>';print_r($comments);exit;
			//echo '<pre>';print_r(date('Y-m-d H:i:s'));exit;
			$this->view->trigger_id=$trigger_id;
			$this->view->getComments=$comments;
			$this->view->type=$type;						
			}
			
			if(strlen($view_result['message']) > 80) {
				$this->view->siteTitle = substr($view_result['message'],0,80).'...';
				$this->view->siteDesc = substr($view_result['message'],0,200).'...';			
			} else {
				$this->view->siteTitle = substr($view_result['message'],0,80);
				$this->view->siteDesc = substr($view_result['message'],0,200);			
			}
	}
	public function deleteTriggerAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$memid=$_SESSION["sess_userid"];
	  	$trigger_id = $_POST['val'];
		$status="Inactive";
		$result = Doctrine::getTable('TriggersRelations')->deleteTrigger($trigger_id,$memid,$status);
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);	
	}
	public function deleteCommentAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$memid=$_SESSION["sess_userid"];
	  	$comment_id = $_POST['comment_id'];
		$result = Doctrine::getTable('Comments')->deleteComment($comment_id);
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);	
	}
	public function shareCommentAction()
	{
		include('../lib/mail/SendEmail.php');
		//echo '<pre>';print_r($_POST);exit;
		$trigger_id=$_POST['val'];
		$comment=$_POST['comment'];
		$memid=$_SESSION["sess_userid"];
		$owner_ids = Doctrine::getTable('TriggersRelations')->getOwnerDetailsByTriggerId($trigger_id);
		$owner_id = array_unique($owner_ids);
		//echo'<pre>';print_r($owner_id);exit;
		if($owner_id['owner_id']!=$memid)
		{
			$notifications = Doctrine::getTable('Notifications')->getNotifications($owner_id['owner_id']);
			//echo'<pre>';print_r($notifications['mytrigger_comments']);exit;
			if($notifications)
			{
			if($notifications[0]['mytrigger_comments'] !="off")
			{
			$user_name = Doctrine::getTable('Users')->getEmail($owner_id['owner_id']);
			$loginLink = $this->CONFIG->serverAddress.'home';
							$Subject = "comment on trigger notification from EmotionsMirror";
							$Message = 'Hello "'.$user_name['first_name'].'",<br/><br/>you have Comment Notification  in your emotionmirror  <br/><br/>
								<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
									Emirror team';
							global $serverAddress;
							sendMail($user_name['email'],$Subject,$Message,$_SESSION["sess_email"]);
			}
			}
		}
		$users = Doctrine::getTable('TriggersRelations')->getUsersDetailsByTriggerId($trigger_id);
		//echo'<pre>';print_r($users);exit;
		foreach($users as $ids)
		{
			//echo'<pre>';print_r($ids['user_id']);exit;
			$notifications = Doctrine::getTable('Notifications')->getNotifications($ids['user_id']);
			//echo'<pre>';print_r($notifications['mytrigger_comments']);exit;
			if($notifications)
		    {
			if($notifications[0]['comments_others_shared'] !="off")
			{
			$user_name = Doctrine::getTable('Users')->getEmail($ids['user_id']);
			//echo'<pre>';print_r($user_name);exit;
			$loginLink = $this->CONFIG->serverAddress.'home';
							$Subject = "comment on trigger notification from EmotionsMirror";
							$Message = 'Hello "'.$user_name['first_name'].'",<br/><br/>you have Comment Notification  in your emotionmirror account<br/><br/>
								<br/><br/><br/>Please click on the following link to login into your account<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
									Emirror team';
							global $serverAddress;
							sendMail($user_name['email'],$Subject,$Message,$_SESSION["sess_email"]);
			}
			}
		}
			$result = Doctrine::getTable('Comments')->addComment($trigger_id,$memid,$comment);
		if($result)
		{
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);	
		}
	}
	public function paginationAction()
	{
		$type=$this->_getParam('id');
		$memid=$_SESSION["sess_userid"];
		$this->view->params ="";
		$currentPage = $this->_getParam('current_page') == 0? 1:$this->_getParam('current_page');
		$resultsPerPage = $this->_getParam('results_per_page') == 0? 12:$this->_getParam('results_per_page');
		$serValue = $this->_getParam('orderby') == ''? '':$this->_getParam('orderby');
		if($type == 1)
		{
		$ideaDetails = Doctrine::getTable('Triggers')->getTriggers($memid,$currentPage,$resultsPerPage,$serValue);
		$this->view->getTriggers = $ideaDetails;
		$jsonData['output'] = $this->view->render('index/personal-list.html');
		}
		else if($type == 2)
		{
			$ideaDetails= Doctrine::getTable('Triggers')->getSharedTriggers($memid,$currentPage,$resultsPerPage,$serValue);
			$this->view->getTriggers = $ideaDetails;
			$jsonData['output'] = $this->view->render('index/shared-list.html');
		}
		else
		{
			$trigger_id=base64_decode($type);
			$ideaDetails = Doctrine::getTable('Comments')->getComments($trigger_id,$currentPage,$resultsPerPage);
			$this->view->getComments = $ideaDetails;
			$jsonData['output'] = $this->view->render('index/comment-list.html');
		}
		$jsonData['pages'] = $ideaDetails['pages'];
	    $jsonData['pager'] = $ideaDetails['pager'];
	    $jsonData['currentPage'] = $ideaDetails['currentPage'];
		$jsonData['resultsCount'] = $ideaDetails['resultsCount'];
		$this->_helper->json->sendJson($jsonData);
		//echo '<pre>';print_r($ideaDetails);exit;
		
		
		
	}
	public function favoriteAction()
	{
		//echo '<pre>';print_r($_POST);exit;
	  	$memid = $_SESSION["sess_userid"];
		$trigger_id=$_POST["trigger_id"];
		$status=$_POST["status"];
		$type=$_POST["type"];
		if($status=="Active")
		{
		 //trig $status;exit;
		$result= Doctrine::getTable('MyFavorites')->addToFavorite($memid,$trigger_id,$type);
		//echo '<pre>';print_r($result);exit;
		}
		else
		{
			if($status=="Inactive")
			{
				//echo "inactive";exit;
				$result= Doctrine::getTable('MyFavorites')->inactiveFavorite($memid,$trigger_id,$type);
				//echo '<pre>';print_r($result);exit;
			}
		}
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);
			
	}
	

}	
	